package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.bridge.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 通用接口
 */
@Api(tags = "通用接口")
@RestController
@Slf4j
@RequestMapping("/admin/common")
public class CommonController {

    @Autowired
//    private AliOssUtil aliOssUtil;
    private MinioUtil minioUtil;

    @ApiOperation("上传文件")
    @RequestMapping("/upload")
    public Result<String> upload(MultipartFile file){
        log.info("上传文件:{}",file);
        //获取原始文件名字
        String originalFilename = file.getOriginalFilename();
        if(originalFilename!=null && !originalFilename.isEmpty()) {
            try {
                //获取拓展名
                String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
                //使用UUID生成文件名字
                String fileName = UUID.randomUUID().toString() + extension;
                //上传文件并获取文件地址
                String filePath = minioUtil.upload(file.getBytes(), fileName);
                return Result.success(filePath);
            } catch (Exception e) {
                log.error("上传失败:{}", file);
            }
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
